<script lang="ts" setup>
import HomeRequest from '@/request/homeRequest';
import to from '@/utils/to';

const newDetails = ref<IObtainNewsDetailRes | undefined>(undefined);

onLoad((options) => {
  if (!options) return;
  handleActions(1, options.id);
});

// 请求资讯详情信息
const fetchObtainNewsDetail = (id: number) => {
  HomeRequest.ObtainNewsDetail({ id }).then((res) => {
    newDetails.value = res.data;
    uni.setNavigationBarTitle({ title: res.data.libraryName });
  });
};

// 资讯浏览量点赞与不喜欢功能状态操作
const handleActions = async (type: number, id: number) => {
  await to(HomeRequest.NewsActionsUpdate({ type, id }));
  fetchObtainNewsDetail(id);
};
</script>
<template>
  <view class="bg-white h-screen c-pd" v-if="newDetails">
    <view class="flex justify-between items-center mb-12px c-text-color-grey c-text-12">
      <view class="flex items-center">浏览数<i class="i-mdi:eye mx-4px"></i>{{ newDetails.viewNums }}</view>
      <view class="flex items-center">
        <view class="flex items-center" @click="handleActions(1, newDetails.libraryId)"><i class="i-mingcute:thumb-up-2-fill" :class="newDetails.upNums < 0 ? 'text-red' : ''"></i>{{ Math.abs(newDetails.upNums) }}</view>
        <view class="flex items-center" :class="newDetails.downNums < 0 ? 'text-red' : ''" @click="handleActions(1, newDetails.libraryId)"><i class="i-mingcute:thumb-down-2-fill ml-8px"></i>{{ Math.abs(newDetails.downNums) }}</view>
      </view>
    </view>
    <UvParse :content="newDetails.content" containerStyle="padding-bottom: 30px" />
  </view>

  <van-empty v-else description="暂无数据" />
</template>
